package com.spark.tags;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.spark.core.SparkContext;
import com.spark.core.XMap;

/**
 * 
 * @version $Id: Radio.java v 1.0 2011-3-19 下午05:29:44 IBM Exp $
 * @author <a href="mailto:njq.niu@hand-china.com">vincent</a>
 */
public class Radio extends ViewComponent {
	
	private static final String DEFAULT_TEMPLATES = "radio.ftl";
	
	private static final String RROPERTITY_ITEMS = "items";
	private static final String PROPERTITY_LABEL_FIELD = "labelField";
	private static final String PROPERTITY_VALUE_FIELD = "valueField";
	private static final String PROPERTITY_LAYOUT = "layout";
	private static final String PROPERTITY_OPTIONS = "options";
//	private static final String PROPERTITY_LABEL_EXPRESSION = "labelexpression";
	
	public static final String DEFAULT_LAYOUT_HORIZONTAL = "horizontal";
	public static final String DEFAULT_LAYOUT_VERTICAL = "vertical";
	public static final String DEFAULT_LABEL_FIELD = "label";
	public static final String DEFAULT_VALUE_FIELD = "value";
	
	
	public String getTemplate() {
		return DEFAULT_TEMPLATES;
	}
	
	public void init(XMap view, SparkContext context) throws Exception {
		super.init(view, context);
		String layout = view.getString(PROPERTITY_LAYOUT,DEFAULT_LAYOUT_HORIZONTAL);
		String labelField = view.getString(PROPERTITY_LABEL_FIELD);
		String valueField = view.getString(PROPERTITY_VALUE_FIELD);
		
		XMap items = view.getChild(RROPERTITY_ITEMS);
		if(items!=null){
			createOptions(context,items,layout,labelField,valueField);
		}else {
			//TODO:从数据库获取
//			String ds = view.getString(PROPERTITY_OPTIONS);
//			if(ds!=null){
//				XMap options = (XMap)model.getObject(ds);
//				if(options!=null)
//				try {
//					createOptions(session,map,options,layout,labelField,valueField, rc.getLabelExpression());
//				} catch (JSONException e) {
//					throw new IOException(e.getMessage());
//				}
//			}
		}
		context.addJsonConfig(PROPERTITY_VALUE_FIELD, valueField);
	}
	
	
	private void createOptions(SparkContext context, XMap items,String layout,String labelField,String valueField) {
		StringBuilder sb = new StringBuilder();
		List<XMap> children = items.getChilds();
		List<XMap> options = new ArrayList<XMap>();
		if(children!=null){
			Iterator<XMap> it = children.iterator();
			int i = 1;
			while(it.hasNext()){
				XMap item = (XMap)it.next();
				String label = item.getString(labelField, "");
				String value = item.getString(valueField, "");
				options.add(item);
				
//				if(!"".equals(label)){
//					label = label;
//				}
				
				sb.append("<div class='s_radio_option'  style='text-align:left;");
				if("horizontal".equalsIgnoreCase(layout)) {
					sb.append("float:left;");
					if(i!=1) sb.append("margin-left:5px;");
				}
				sb.append("'");				
				
				sb.append(" itemvalue='").append(value).append("'>"); 
				sb.append("<div class='s_radio'></div>");
				sb.append("<div class='s_radio_lb'>").append(label).append("</div>");
				sb.append("</div>");
				i++;
			}
		}
		context.addJsonConfig(PROPERTITY_OPTIONS, options);
		context.put(PROPERTITY_OPTIONS, sb.toString());
	}
	
	@SuppressWarnings("unchecked")
	protected String build(SparkContext context){
		Map map = context.getMap();
		StringBuilder sb = new StringBuilder();
		sb.append("<DIV class=\"").append(get(map,"className")).append("\" style=\"background:none;line-height:20px;height:20px;width:").append(get(map,"width")).append("px;").append(get(map,"style")).append("\" id=\"").append(get(map,"id")).append("\">").append(get(map,"options")).append("</DIV>\r");
		sb.append("<script>new Spark.Radio(").append(get(map,"config")).append(");</script>\r");
		return sb.toString();
	}

}
